package com.dddd.blog.blogfile.controller;

import com.dddd.blog.blogcommon.constant.ResultCode;
import com.dddd.blog.blogcommon.pojo.Result;
import com.dddd.blog.blogcommon.utils.ResultUtils;
import com.dddd.blog.blogfile.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 * @author 喵星球的大橘
 */
@RestController
@RequestMapping("/upload")
@CrossOrigin(origins = "*",allowedHeaders = "*",methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @PostMapping("/image")
    public Result uploadSimpleImage(MultipartFile file){

        if(file.isEmpty()){
            return ResultUtils.error(ResultCode.PARAMS_BLANK);
        }

        String url = uploadService.uploadImage(file);
        if(!StringUtils.isEmpty(url)){
            return ResultUtils.success(url);
        }
        return ResultUtils.error(ResultCode.FAIL);
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }
}
